from flaskapp.exts import db


class Message(db.Model):
    __tablename__ = 'messages'

    msg_id = db.Column(db.Integer, primary_key=True)
    to = db.Column(db.Integer)
    message = db.Column(db.Text)
    date = db.Column(db.DateTime)

    def __init__(self, to, message, date):
        self.to = to
        self.message = message
        self.date = date
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return '<Message %r>' % self.message

    def to_dict(self):
        return {
            'msg_id': self.msg_id,
            'to': self.to,
            'message': self.message,
            'date': self.date
        }

